
pkgname=openssh
pkgver=7.7p1
pkgrel=1
pkgdesc='Free version of the SSH connectivity tools'
arch=('x86_64')
license=('custom:BSD')
url='https://www.openssh.org/portable.html'
backup=('etc/ssh/ssh_config' 'etc/ssh/sshd_config' 'etc/pam.d/sshd')
depends=('krb5' 'openssl' 'libedit')
makedepends=('linux-headers')
source=("https://ftp.openbsd.org/pub/OpenBSD/OpenSSH/portable/${pkgname}-${pkgver}.tar.gz"
        'sshd.confd'
        'sshd.pam'
        'sshdgenkeys.service'
        'sshd.service'
        'sshd@.service'
        'sshd.socket')
md5sums=('68ba883aff6958297432e5877e9a0fe2'
         'e2cea70ac13af7e63d40eb04415eacd5'
         '9d71ea52cd15e9d7c172b7b71cb03bc0'
         '234ac4883cc28c56a4c236867124c3e4'
         '4bbc5818d93a2ad336d7191ed35c69f7'
         'fdc4a47b4b8d0f0e2c395c9edcee8c14'
         '76f52c66fb3193f301fe0a666e047ab1')

build() {
	cd ${pkgname}-${pkgver}

	./configure \
		--prefix=/usr \
		--libexecdir=/usr/lib/ssh \
		--sysconfdir=/etc/ssh \
		--with-libedit \
		--with-privsep-user=nobody \
		--with-md5-passwords \
		--with-pam \
		--with-mantype=man \
		--with-xauth=/usr/bin/xauth \
		--with-kerberos5=/usr \
		--with-ssl-engine \
		--with-libedit \
		--with-pid-dir=/run \

	make
}

check() {
	cd ${pkgname}-${pkgver}

	make tests ||
	grep $USER /etc/passwd | grep -q /bin/false
}

package() {
	cd ${pkgname}-${pkgver}
	make DESTDIR=${pkgdir} install

	install -Dm644 ../sshd.pam ${pkgdir}/etc/pam.d/sshd
	install -Dm644 ../sshd.confd ${pkgdir}/etc/conf.d/sshd

	install -Dm644 LICENCE ${pkgdir}/usr/share/licenses/${pkgname}/LICENCE

	# scripts
	install -Dm755 contrib/findssl.sh ${pkgdir}/usr/bin/findssl.sh
	install -Dm755 contrib/ssh-copy-id ${pkgdir}/usr/bin/ssh-copy-id
	install -Dm644 contrib/ssh-copy-id.1 ${pkgdir}/usr/share/man/man1/ssh-copy-id.1

	# PAM feature 
	sed -i	-e '/^#ChallengeResponseAuthentication yes$/c ChallengeResponseAuthentication no' \
	        -e '/^#PrintMotd yes$/c PrintMotd no # pam does that' \
		-e '/^#UsePAM no$/c UsePAM yes' \
		${pkgdir}/etc/ssh/sshd_config

	install -Dm644 ../sshdgenkeys.service ${pkgdir}/usr/lib/systemd/system/sshdgenkeys.service
	install -Dm644 ../sshd@.service ${pkgdir}/usr/lib/systemd/system/sshd@.service
	install -Dm644 ../sshd.service ${pkgdir}/usr/lib/systemd/system/sshd.service
	install -Dm644 ../sshd.socket ${pkgdir}/usr/lib/systemd/system/sshd.socket
}
